package com.LeetCode.List;


import org.junit.Test;

public class ReverseListBetweenNM {

    public ListNode reverseBetween(ListNode head, int m, int n) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode preStart = dummy;
        ListNode start = head;
        for(int i = 1; i<m; i++){
            preStart = start;
            start = start.next;
        }

        for(int i = 0; i<n - m; i++){
            ListNode next = start.next;
            start.next = next.next;
            next.next = preStart.next;
            preStart.next = next;
        }
        return dummy.next;
    }

    @Test
    public void test(){
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        ListNode node5 = new ListNode(5);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        ListNode.printList(reverseBetween(node1, 2, 4));
    }
}
